﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Globalization;


namespace VISS.Web.Frontend
{
    public enum eLANGUAGE { NONE, EN, VI }
    public class Pager
    {
        #region Member Variables
        protected int m_visibleInitLeftPages = 5;
        protected int m_visibleLeftOne = 1;
        protected int m_visibleRightOne = 1;
        protected int m_visibleLeftPages = 2;
        protected int m_visibleRitePages = 2;
        //protected string m_multiPageSymbols = "<li class=\"pagination\" style=\" font-family:Arial; font-size:12px; font-weight:bold; padding-top:15px  \" >...</li>"; //can be replace by <img/> or <span/> tag
        protected string m_multiPageSymbols = "<img style=\" margin-bottom:-6px \" src=\""+"\" />"; //can be replace by <img/> or <span/> tag
        protected string m_prevButton = "<< Prev";//"« Prev"; //can be replace by <img/> tag
        protected string m_nextButton = "Next >>";//"Next »"; //can be replace by <img/> tag
        protected string m_firstButton = "<< Previous";
        protected string m_lastButton = "Last >>";
        protected string m_cssClass = "";
        protected string m_urlLink = "";

        private eLANGUAGE m_Language = eLANGUAGE.EN;
        private int m_totalSize;
        private int m_pageSize;
        private int m_pageNum;
        //Quy added 20080829 Grouping digit: "Displaying 31 to 45 of 49,485 results" 
        // changed 20090410 (feedback): Please change text to read “1 – 30 of 103 results” everywhere results are displayed.
        protected string sNumFormat = "#,###";
        //protected NumberFormatInfo nFI = new CultureInfo("en-US", false).NumberFormat;  

        private string sHref = "[HREF_PAGE_NUM]";
        private string sOnclick = "[CLICK_FUNCTION]";
        #endregion

        #region Contructors
        public Pager(int pvTotalSize, int pvPageSize, int pvPageNum, string pvBaseUrl)
        {
            m_totalSize = pvTotalSize;
            m_pageSize = pvPageSize;
            m_pageNum = pvPageNum;
            m_urlLink = pvBaseUrl;

            if (m_totalSize < 0) m_totalSize = 0;
            if (m_pageSize <= 0) m_pageSize = 15;
            if (m_pageNum <= 0) m_pageNum = 1;
            if (m_urlLink.IndexOf("?") < 4) m_urlLink += "?";
            if (m_urlLink.EndsWith("?")) m_urlLink += "0=1";
        }
        #endregion

        #region Properties
        public int TotalSize { get { return m_totalSize; } }
        public int PageSize { get { return m_pageSize; } }
        public int PageNum { get { return m_pageNum; } }
        public int TotalPages
        {
            get
            {
                if (TotalSize <= PageSize) return 1;
                int pages = (TotalSize - (TotalSize % PageSize)) / PageSize;
                return (TotalSize % PageSize > 0 ? pages + 1 : pages);
            }
        }
        public int StartIndex
        {
            get
            {
                if (TotalSize > 0) return (PageNum - 1) * PageSize + 1;
                return 1;
            }
        }
        public int EndIndex
        {
            get
            {
                if (TotalSize > 0) return (StartIndex + m_pageSize - 1 > TotalSize ? TotalSize : StartIndex + m_pageSize - 1);
                return 0;
            }
        }
        public string CssClass
        {
            get { return m_cssClass; }
            set { m_cssClass = value; }
        }
        public eLANGUAGE Language
        {
            get { return m_Language; }
            set { m_Language = value; }
        }

        //Trung added 20080829
        public string TotalSizeString { get { return m_totalSize.ToString(sNumFormat); } }
        public string StartIndexString { get { return StartIndex.ToString(sNumFormat); } }
        public string EndIndexString { get { return EndIndex.ToString(sNumFormat); } }
        #endregion

        #region Public Methods
        public virtual string DisplayResultInfo()
        {
            StringBuilder sb = new StringBuilder();
            string sResult = "results";
            string s_Total = TotalSize > 1000 ? "1000+" : TotalSize.ToString(sNumFormat);

            if (TotalSize > 0)
            {
                if (TotalSize == 1) sResult = "result";
                sb.AppendFormat("<span class=\"{0}pagerinfo\" >", CssClass);
                if (Language == eLANGUAGE.VI)
                    sb.AppendFormat("Hiển thị {0} đến {1} của {2} kết quả", StartIndex, EndIndex, s_Total);
                else
                {
                    if (TotalSize > 1000 && EndIndex == TotalSize)
                    {
                        sb.AppendFormat("<b>Last Page</b> of&nbsp;<b>{0}</b> " + sResult, s_Total);
                    }
                    else
                    {
                        sb.AppendFormat("<b>Displaying &nbsp; {0}</b> - <b>{1}</b> of&nbsp;<b>{2}</b> " + sResult, StartIndex.ToString(sNumFormat), EndIndex.ToString(sNumFormat), s_Total);
                    }
                }
                sb.Append("</span>");
            }
            return sb.ToString();
        }
        public virtual string MyBodyPaging()
        {
            StringBuilder sb = new StringBuilder();
            string sResult = "entries";
            string s_Total = TotalSize > 1000 ? "1000+" : TotalSize.ToString(sNumFormat);

            if (TotalSize > 0)
            {
                if (TotalSize == 1) sResult = "entry";
                sb.AppendFormat("<span class=\"{0}pagerinfo\" >", CssClass);
                if (Language == eLANGUAGE.VI)
                    sb.AppendFormat("Hiển thị {0} đến {1} của {2} kết quả", StartIndex, EndIndex, s_Total);
                else
                {
                    if (TotalSize > 1000 && EndIndex == TotalSize)
                    {
                        sb.AppendFormat("Last Page of&nbsp;{0} " + sResult, s_Total);
                    }
                    else
                    {
                        sb.AppendFormat("Showing &nbsp; <b>{0}</b> - <b>{1}</b> of&nbsp;<b>{2}</b> " + sResult, StartIndex.ToString(sNumFormat), EndIndex.ToString(sNumFormat), s_Total);
                    }
                }
                sb.Append("</span>");
            }
            return sb.ToString();
        }
        public virtual string DisplayPaging()
        {
            StringBuilder sb = new StringBuilder();
            if (TotalSize > PageSize)
            {
                sb.AppendFormat("<ul class=\"{0}\">", CssClass);
                //sb.Append("<ul >");
                sb.Append(GetPagingItem(0));

                int leftPageNum;
                int ritePageNum;
                string ritePaging = String.Empty;

                if (PageNum < m_visibleInitLeftPages) m_visibleLeftPages = m_visibleInitLeftPages;

                //Append First number (left) length = m_visibleFirstPages
                for (leftPageNum = 1; leftPageNum <= m_visibleLeftPages && leftPageNum <= TotalPages; leftPageNum++)
                    sb.AppendFormat("{0}", GetPagingItem(leftPageNum));

                //Append last number (right) length = m_visibleEndPages
                for (ritePageNum = TotalPages; ritePageNum > (TotalPages - m_visibleRitePages) && ritePageNum >= leftPageNum; ritePageNum--)
                    ritePaging = String.Format("{1}{0}", ritePaging, GetPagingItem(ritePageNum));

                if (leftPageNum <= ritePageNum)
                {
                    //int innerPageNum = PageNum - m_visibleLeftPages;
                    int innerPageNum = PageNum - m_visibleLeftOne;
                    if (innerPageNum <= leftPageNum) innerPageNum = leftPageNum;
                    else sb.Append(m_multiPageSymbols);

                    //for (; innerPageNum <= PageNum + m_visibleRitePages && innerPageNum <= ritePageNum; innerPageNum++)
                    for (; innerPageNum <= PageNum + m_visibleRightOne && innerPageNum <= ritePageNum; innerPageNum++)
                        sb.AppendFormat("{0}", GetPagingItem(innerPageNum));

                    if (innerPageNum <= ritePageNum) sb.Append(m_multiPageSymbols);
                }

                sb.Append(ritePaging);
                sb.Append(GetPagingItem(-1));
                sb.Append("</ul>");
            }
            return sb.ToString();
        }

        public virtual string DisplayPagingHistory()
        {
            StringBuilder sb = new StringBuilder();
            if (TotalSize > PageSize)
            {
                sb.AppendFormat("<ul class=\"{0}\">", CssClass);
                //sb.Append("<ul >");
                sb.Append(GetPagingHistoryItem(0));

                int leftPageNum;
                int ritePageNum;
                string ritePaging = String.Empty;

                if (PageNum < m_visibleInitLeftPages) m_visibleLeftPages = m_visibleInitLeftPages;

                //Append First number (left) length = m_visibleFirstPages
                for (leftPageNum = 1; leftPageNum <= m_visibleLeftPages && leftPageNum <= TotalPages; leftPageNum++)
                    sb.AppendFormat("{0}", GetPagingHistoryItem(leftPageNum));

                //Append last number (right) length = m_visibleEndPages
                for (ritePageNum = TotalPages; ritePageNum > (TotalPages - m_visibleRitePages) && ritePageNum >= leftPageNum; ritePageNum--)
                    ritePaging = String.Format("{1}{0}", ritePaging, GetPagingHistoryItem(ritePageNum));

                if (leftPageNum <= ritePageNum)
                {
                    //int innerPageNum = PageNum - m_visibleLeftPages;
                    int innerPageNum = PageNum - m_visibleLeftOne;
                    if (innerPageNum <= leftPageNum) innerPageNum = leftPageNum;
                    else sb.Append(m_multiPageSymbols);

                    //for (; innerPageNum <= PageNum + m_visibleRitePages && innerPageNum <= ritePageNum; innerPageNum++)
                    for (; innerPageNum <= PageNum + m_visibleRightOne && innerPageNum <= ritePageNum; innerPageNum++)
                        sb.AppendFormat("{0}", GetPagingHistoryItem(innerPageNum));

                    if (innerPageNum <= ritePageNum) sb.Append(m_multiPageSymbols);
                }

                sb.Append(ritePaging);
                sb.Append(GetPagingHistoryItem(-1));
                sb.Append("</ul>");
            }
            return sb.ToString();
        }

        /// <summary>
        /// Show link javascript for load paging ajax
        /// Change http://... to javascript:LoadPage(PageNum)
        /// Ex : href="javascript:LoadPage(1)" 
        /// </summary>
        /// <returns>javascript:LoadPage(PageNum)</returns>
        public virtual string DisplayPagingJs()
        {
            int numpage = 0;
            string sPaging = "";

            //remove all query string
            m_urlLink = "";
            sPaging = DisplayPaging();

            for (int i = TotalPages; i > 0; i--)
            {
                sPaging = sPaging.Replace("&PageSize=" + PageSize + "&PageNum=" + i, "javascript:LoadPage(" + i + ")");
            }

            return sPaging;
        }

        /// <summary>
        /// Show link javascript for load paging ajax
        /// Change http://... to javascript:JsFunctionName(PageNum)
        /// Ex : href="javascript:JsFunctionName(1)" 
        /// </summary>
        /// <param name="functionJsName">Ex:LoadAjaxPage</param>
        /// <returns></returns>
        public virtual string DisplayPagingJs(string JsFunctionName)
        {
            int numpage = 0;
            string sPaging = "";

            //remove all query string
            m_urlLink = "";
            sPaging = DisplayPaging();

            for (int i = TotalPages; i > 0; i--)
            {
                sPaging = sPaging.Replace("&PageSize=" + PageSize + "&PageNum=" + i, "javascript:" + JsFunctionName + "(" + i + ")");
            }

            return sPaging;
        }

        /// <summary>
        /// Show link javascript for load paging ajax
        /// Change http://... to #page1 and add onclick with function
        /// Ex : href="#page1" 
        /// </summary>
        /// <param name="functionJsName">Ex:LoadAjaxPage</param>
        /// <returns></returns>
        public virtual string DisplayPagingHistoryJs(string JsFunctionName)
        {
            int numpage = 0;
            string sPaging = "";

            //remove all query string
            m_urlLink = "";
            sPaging = DisplayPagingHistory();

            for (int i = TotalPages; i > 0; i--)
            {
                sPaging = sPaging.Replace(sHref + "_" + i.ToString(), "#page" + i.ToString()).Replace(sOnclick + "_" + i.ToString(), JsFunctionName + "(" + i.ToString() + ")");
            }

            return sPaging;
        }

        #endregion

        #region Protected Methods
        protected virtual string GetPagingItem(int pagenum)
        {
            string html = String.Empty;
            int charsWidth = (6 * pagenum.ToString().Length);
            int realCharsWidth = (7 * pagenum.ToString().Length);

            int iWidth = (12 + charsWidth);
            int iPaddingLeft = (iWidth - realCharsWidth) / 2;
            int iPaddingRight = (iWidth - realCharsWidth) - iPaddingLeft;

            if (pagenum == 0 && PageNum > 1) //PREV button
                html = String.Format("<li><a href=\"{0}\">{1}</a></li>", GetLink(PageNum - 1), m_prevButton);
            else if ((pagenum == -1 || pagenum > TotalPages) && PageNum < TotalPages) //NEXT button
                html = String.Format("<li><a href=\"{0}\">{1}</a></li>", GetLink(PageNum + 1), m_nextButton);
            else if (pagenum >= 1 && pagenum <= TotalPages)
            {
                html = (pagenum == PageNum ? "{2}" : "<a href=\"{1}\">{2}</a>");
                html = String.Format("<li {0}>" + html + "</li>", (pagenum == PageNum ? " class=\"Allli_select\"" : ""), GetLink(pagenum), pagenum);
            }


            return html;
        }
        protected virtual string GetPagingHistoryItem(int pagenum)
        {
            string html = String.Empty;
            int charsWidth = (6 * pagenum.ToString().Length);
            int realCharsWidth = (7 * pagenum.ToString().Length);

            int iWidth = (12 + charsWidth);
            int iPaddingLeft = (iWidth - realCharsWidth) / 2;
            int iPaddingRight = (iWidth - realCharsWidth) - iPaddingLeft;

            if (pagenum == 0 && PageNum > 1) //PREV button
                html = String.Format("<li><a href=\"{0}\" rel=\"history\" onclick=\"{1}\" style=\"background-color:Transparent; color:#1C496A ; font-weight:bold; font-size:14px;  border-style:none \">{2}</a></li>"
                    , sHref + "_" + (PageNum - 1).ToString(), sOnclick + "_" + (PageNum - 1).ToString(), m_prevButton);
            else if ((pagenum == -1 || pagenum > TotalPages) && PageNum < TotalPages) //NEXT button
                html = String.Format("<li><a href=\"{0}\" rel=\"history\" onclick=\"{1}\" style=\"background-color:Transparent; color:#1C496A ; font-weight:bold; font-size:14px; padding-right:0px; border-style:none \">{2}</a></li>"
                    , sHref + "_" + (PageNum + 1).ToString(), sOnclick + "_" + (PageNum + 1).ToString(), m_nextButton);
            else if (pagenum >= 1 && pagenum <= TotalPages)
            {
                html = String.Format("<li class=\"{0}\"><a href=\"{1}\" rel=\"history\" onclick=\"{2}\" style=\"text-decoration:{3}{4}; {6}; padding-left: {7}; padding-right: {8};  \">{5}</a></li>",
                        (pagenum == PageNum ? "current" : ""),
                        sHref + "_" + pagenum.ToString(), sOnclick + "_" + pagenum.ToString(),
                       (pagenum == PageNum ? "none" : "none"),
                       (pagenum == PageNum ? "; font-weight:bold" : ""),
                       pagenum,
                    //(pagenum == PageNum ? "background-color:#FFCC00" : ""),
                       (pagenum == PageNum ? "background-color:#1C496A; color:#FFFFFF; border:#123147 solid 1px" : ""),
                        iPaddingLeft + "px", iPaddingRight + "px");
            }


            return html;
        }
        protected virtual string GetLink(int pagenum)
        {
            return String.Format("{0}&PageSize={1}&PageNum={2}", m_urlLink.Trim("&".ToCharArray()), PageSize, pagenum);
        }
        #endregion


      
    }
        
}
